﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Photography.App_Data;

namespace Photography.Admin
{
    public partial class AddGallery : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!(Master as Photography.Admin.AdminMaster).AdminID.HasValue)
                Response.Redirect("Default.aspx");

            if (!IsPostBack && !string.IsNullOrEmpty(Request["GalleryID"]))
            {
                btnSave.Text = "Save Gallery";

                Photography.App_Data.PhotographyDataContext context = new Photography.App_Data.PhotographyDataContext();

                GetPublicGalleriesResult gallery = (from a in context.GetPublicGalleries() where a.ID == int.Parse(Request["GalleryID"]) select a).First();
                txtName.Text = gallery.Name;
                txtDescription.Text = gallery.Description;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Photography.App_Data.PhotographyDataContext context = new Photography.App_Data.PhotographyDataContext();

            if (string.IsNullOrEmpty(Request["GalleryID"]))
                context.AddGallery(txtName.Text, null, null, txtDescription.Text, null);
            else
                context.UpdateGallery(int.Parse(Request["GalleryID"]), txtName.Text, null, null, txtDescription.Text, null);

            Response.Redirect("ManageGalleries.aspx?GalleryID=" + (string.IsNullOrEmpty(Request["GalleryID"]) ? "" : Request["GalleryID"]));
        }

    }
}
